package com.example.brain2;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

public class Image {

	private ImageView frontPicture;
	private ImageView backPicture;

	
	private boolean isTurnedToFront;

	public Image(ImageView frontPicture, ImageView backPicture) {
		super();
		this.frontPicture = frontPicture;
		this.backPicture = backPicture;
		this.isTurnedToFront = false;
	}

	public Image(ImageView frontPicture, ImageView backPicture,
			boolean isTurendFront) {
		super();
		this.frontPicture = frontPicture;
		this.backPicture = backPicture;
		this.isTurnedToFront = isTurendFront;
	}

	//getter/setter for FRONT
	public ImageView getFrontPicture() {
		return frontPicture;
	}

	public void setFrontPicture(ImageView frontPicture) {
		this.frontPicture = frontPicture;
	}

	//getter/setter for BACK
	public ImageView getBackPicture() {
		return backPicture;
	}

	public void setBackPicture(ImageView backPicture) {
		this.backPicture = backPicture;
	}

	//getter/setter for ISTURNED
	public boolean getIsTurnedToFront() {
		return isTurnedToFront;
	}

	public void setTurnedToFront(boolean isTurnedToFront) {
		this.isTurnedToFront = isTurnedToFront;
	}

	//returns View by taking the id-s of back and front images 
	public View getBackImageId() {
		return frontPicture.findViewById(R.id.front_image);
	}
	
	public View getFrontImageId() {
		return backPicture.findViewById(R.id.back_image);
	}
	
	////////////////
	public void flipImage() {
		rotate(0, 80);
	}

	public void rotate(float start, float end) {
		applyRotation(start, end, frontPicture, backPicture, isTurnedToFront);
		isTurnedToFront = !isTurnedToFront;
	}

	private void applyRotation(float start, float end, ImageView frontImage,
			ImageView image2, boolean fronImage) {
		// Find the center of image
		final float centerX = frontImage.getWidth() / 2.0f;
		final float centerY = frontImage.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip rotation = new Flip(start, end, centerX, centerY);
		rotation.setDuration(300);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new SwitchImages(fronImage, frontImage,
				image2));

		if (fronImage) {
			frontImage.startAnimation(rotation);
		} else {
			image2.startAnimation(rotation);
		}
		
		

	}

}
